player batting

Code
library(tidyverse)
library(ggplot2)
Code
batting_2019 <- read.csv('player_batting_2019.csv')
batting_2023 <- read.csv('player_batting_2023.csv')

df_2019 <- batting_2019 %>%
  filter(Tm=='WSN')%>%
  select(Name,Tm,PA,BA,OBP,OPS.,SLG,H.)%>%
  arrange(desc(H.))%>%
  na.omit()%>%
  slice_head(n=20)

df_2023 <- batting_2023 %>%
  filter(Tm =='WSN')%>%
  select(Name,Tm,PA,BA,OBP,OPS.,SLG,H)%>%
  arrange(desc(H))%>%
  na.omit()%>%
  slice_head(n=20)
Code
df_2019_norm <- df_2019 %>%
  mutate(across(.cols = c(BA,OBP,OPS.,SLG,H.), 
                .fns = ~ (. - min(.)) / (max(.) - min(.))))
Code
df_2023_norm <- df_2023 %>%
  mutate(across(.cols = c(BA,OBP,OPS.,SLG,H), 
                .fns = ~ (. - min(.)) / (max(.) - min(.))))
Code
library(plotly)

theta_labels= c('Batting Average', 'On-base percentage','On-base Plus Slugging Plus','Slugging','Hits')

fig <- plot_ly(
    type = 'scatterpolar',
    fill = 'toself'
  ) 
fig <- fig %>%
  add_trace(
    r = unlist(as.numeric(df_2019_norm[1,4:8])),
    theta = theta_labels,
    fillcolor = 'rgba(255, 0, 0, 0.3)',
    line = list(color = 'rgba(255, 0, 0, 0.5)'),
    hoverinfo='text',
    hovertext = ~paste("2019","<br>Name:",df_2019$Name[1], "<br>Stat:",colnames(df_2023[4:8]), c(df_2019[1,4:8])),
    name = '2019'
  ) 
fig <- fig %>%
  add_trace(
    r = unlist(as.numeric(df_2023_norm[1,4:8])),
    theta = theta_labels,
    fillcolor = 'rgba(0, 0, 255, 0.3)',
    line = list(color = 'rgba(0, 0, 255, 0.5)'),
    hoverinfo='text',
    hovertext= ~paste("2023","<br>Name:",df_2023$Name[1],"<br>Stat:",colnames(df_2023[4:8]), c(df_2023[1,4:8])),
    name = '2023'
  ) 

fig <- fig %>%
  add_trace(
    r = unlist(as.numeric(df_2019_norm[2,4:8])),
    theta = theta_labels,
    fillcolor = 'rgba(255, 0, 0, 0.3)',
    line = list(color = 'rgba(255, 0, 0, 0.5)'),
    hoverinfo='text',
    hovertext= ~paste("2019","<br>Name:",df_2019$Name[2],"<br>Stat:",colnames(df_2019[4:8]), c(df_2019[2,4:8])),
    name = '2019',
    visible=F
  )

fig <- fig %>%
  add_trace(
    r = unlist(as.numeric(df_2023_norm[2,4:8])),
    theta = theta_labels,
    fillcolor = 'rgba(0, 0, 255, 0.3)',
    line = list(color = 'rgba(0, 0, 255, 0.5)'),
    hoverinfo='text',
    hovertext=paste("2023","<br>Name:",df_2023$Name[2],"<br>Stat:",colnames(df_2023[4:8]), c(df_2023[2,4:8])),
    name = '2023',
    visible=F
  ) 

fig <- fig %>%
  add_trace(
    r = unlist(as.numeric(df_2019_norm[3,4:8])),
    theta = theta_labels,
    fillcolor = 'rgba(255, 0, 0, 0.3)',
    line = list(color = 'rgba(255, 0, 0, 0.5)'),
    hoverinfo='text',
    hovertext=paste("2019","<br>Name:",df_2019$Name[3],"<br>Stat:",colnames(df_2019[4:8]), c(df_2019[3,4:8])),
    name = '2019',
    visible=F
  )

fig <- fig %>%
  add_trace(
    r = unlist(as.numeric(df_2023_norm[3,4:8])),
    theta = theta_labels,
    fillcolor = 'rgba(0, 0, 255, 0.3)',
    line = list(color = 'rgba(0, 0, 255, 0.5)'),
    hoverinfo='text',
    hovertext=paste("2023","<br>Name:",df_2023$Name[3],"<br>Stat:",colnames(df_2023[4:8]),c(df_2023[3,4:8])),
    name = '2023',
    visible=F
  )

fig <- fig %>%
  add_trace(
    r = unlist(as.numeric(df_2019_norm[4,4:8])),
    theta = theta_labels,
    fillcolor = 'rgba(255, 0, 0, 0.3)',
    line = list(color = 'rgba(255, 0, 0, 0.5)'),
    hoverinfo='text',
    hovertext=paste("2019","<br>Name:",df_2019$Name[4],"<br>Stat:",colnames(df_2019[4:8]), c(df_2019[4,4:8])),
    name = '2019',
    visible=F
  )

fig <- fig %>%
  add_trace(
    r = unlist(as.numeric(df_2023_norm[4,4:8])),
    theta = theta_labels,
    fillcolor = 'rgba(0, 0, 255, 0.3)',
    line = list(color = 'rgba(0, 0, 255, 0.5)'),
    hoverinfo='text',
    hovertext=paste("2023","<br>Name:",df_2023$Name[4],"<br>Stat:",colnames(df_2023[4:8]), c(df_2023[4,4:8])),
    name = '2023',
    visible=F
  )

fig <- fig %>%
  add_trace(
    r = unlist(as.numeric(df_2019_norm[5,4:8])),
    theta = theta_labels,
    fillcolor = 'rgba(255, 0, 0, 0.3)',
    line = list(color = 'rgba(255, 0, 0, 0.5)'),
    hoverinfo='text',
    hovertext=paste("2019","<br>Name:",df_2019$Name[5],"<br>Stat:",colnames(df_2019[4:8]), c(df_2019[5,4:8])),
    name = '2019',
    visible=F
  )

fig <- fig %>%
  add_trace(
    r = unlist(as.numeric(df_2023_norm[5,4:8])),
    theta = theta_labels,
    fillcolor = 'rgba(0, 0, 255, 0.3)',
    line = list(color = 'rgba(0, 0, 255, 0.5)'),
    hoverinfo='text',
    hovertext=paste("2023","<br>Name:",df_2023$Name[5],"<br>Stat:",colnames(df_2023[4:8]),c(df_2023[5,4:8])),
    name = '2023',
    visible=F
  )

fig <- fig %>%
  layout(
    polar = list(
      radialaxis = list(
        range = c(0,1)
      )
    )
  )
Code
final_plot <- fig %>% layout(
  title = 'Washington Nationals Top 5 Batters 2019 vs 2023',
  updatemenus = list(
    list(
      type = 'buttons',
      direction = 'left',
      x = 0,
      xanchor = 'left',
      y = -0.2,
      yanchor = 'top',
      buttons = list(
        list(method = 'update', args = list(list(visible = c(T, T, T, F, F, F,F, F,F, F,F)), list(title = 'Batter 1')), label = '1st Batter'),
        list(method = 'update', args = list(list(visible = c(T,F, F, T, T, F,F, F,F, F,F)), list(title = 'Batter 2')), label = '2nd Batter'),
        list(method = 'update', args = list(list(visible = c(T,F, F, F, F, T,T, F,F, F,F)), list(title = 'Batter 3')), label = '3rd Batter'),
        list(method = 'update', args = list(list(visible = c(T,F, F, F, F, F,F, T,T, F,F)), list(title = 'Batter 4')), label = '4th Batter'),
        list(method = 'update', args = list(list(visible = c(T,F, F, F, F, F,F, F,F, T,T)), list(title = 'Batter 5')), label = '5th Batter')
      )
    )
  )
)

final_plot